﻿using ChurchDiary.Common;
using ChurchDiary.DAL.Contract;
using ChurchDiary.DAL.DAL;
using ChurchDiary.TableEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChurchDiary.DAL.Methods
{
    
    public class DAL_Dashboard : DataAccess, IDashboard
    {

        public EntityOrganizationDashboard GetOrganizationDashboard()
        {
            EntityOrganizationDashboard OrganizationDashboard = new EntityOrganizationDashboard();
            try
            {

                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_OrganizationDashboard]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@Mode", DbType.String, "BYORGANIZATION");
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return OrganizationDashboard;
                            }
                            while (OReader.Read())
                            {
                                OrganizationDashboard.ChurchCount = (int)OReader["ChurchCounts"];
                            }
                            OReader.NextResult();
                            while (OReader.Read())
                            {
                                OrganizationDashboard.TotalPastors = (int)OReader["PastorsCounts"];
                            }
                            OReader.NextResult();
                            while (OReader.Read())
                            {
                                OrganizationDashboard.TotalMembers = (int)OReader["MembersCounts"];
                            }
                            OReader.NextResult();
                            while (OReader.Read())
                            {
                                OrganizationDashboard.TotalFamily = (int)OReader["FamilyCounts"];
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return OrganizationDashboard;
        }
        public EntityChurchDashboard GetChurchBoard()
        {
            EntityChurchDashboard ChurchBoard = new EntityChurchDashboard();
            try
            {

                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_OrganizationDashboard]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@Mode", DbType.String, "BYCHURCH");
                    SQLServer.AddInParameter(dbCommand, "@ChurchId", DbType.Guid, Current.ChurchId);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return ChurchBoard;
                            }
                            while (OReader.Read())
                            {
                                ChurchBoard.ActiveFamily = (int)OReader["ACTIVEFAMILY"];
                            }
                            OReader.NextResult();
                            while (OReader.Read())
                            {
                                ChurchBoard.ActiveFamilyMembers = (int)OReader["ACTIVEMEMBERS"];
                            }
                            OReader.NextResult();
                            while (OReader.Read())
                            {
                                ChurchBoard.BaptisedMembers = (int)OReader["BAPTISIZED"];
                            }
                            OReader.NextResult();
                            while (OReader.Read())
                            {
                                ChurchBoard.NonBaptisedMembers = (int)OReader["NONBAPTISIZED"];
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return ChurchBoard;
        }
    }
}
